
const _login = require("./login.js");

var rootDocment = 'https://asc.guxinedu.cn/api/';
// var rootDocment = 'http://ascresearch.org:8080/api/';
// var token = '75c8ebac99a801ccb0b9d1d51ec0ff71';
//var token = '06fd261f96099e05a413b490c545d9bb';

//var exports={}; //所有要暴漏的字段参数

/************************暴漏请求的接口名**************************** */

exports.wxLogin = rootDocment + "Login/wxLogin";  //小程序登陆接口
exports.orderList = rootDocment + "User/orderList";  //订单列表
exports.center = rootDocment +  "User/center";  //个人中心页面
exports.myDetail = rootDocment +  "User/detail";  //个人信息详情Detail
exports.myMessage = rootDocment +  "User/message";  //消息列表
exports.banner = rootDocment +  "Project/banner";  //banner
exports.projectCate = rootDocment +  "Project/projectCate";  //课程分类
exports.projectList = rootDocment +  "Project/projectList";  //课程列表
exports.projectDetail = rootDocment +  "Project/projectDetail";  //课程详情

exports.addOrder = rootDocment +  "User/addOrder";  //添加订单
exports.payment = rootDocment +  "User/payment";  //支付页面
exports.wxPay = rootDocment +  "Pay/wxPay";  //微信支付
exports.orderList = rootDocment +  "User/orderList";  //订单列表
exports.teacherList = rootDocment +  "Teacher/united";  //导师列表
exports.tutorClass = rootDocment +  "Teacher/tutorClass";  //导师的课程列表
exports.tutorInfo = rootDocment +  "Teacher/tutorInfo";  //导师详情

exports.enrollHo = rootDocment +  "User/enrollHo";  //普通报名

exports.enrollDes = rootDocment +  "Index/enrollDes";  //夏季报名介绍
exports.enroll = rootDocment +  "User/enroll";  //夏季报名
exports.enrollStatus = rootDocment +  "User/enrollStatus";  //获取报名状态

exports.uploadImg = rootDocment +   "Index/uploadImg"; //上传图片

let u = "123";

/************************请求的接口名**************************** */


/**
 * url 330
 *  data
 *  cb : 返回回调函数
 *  例 reqPost(url, data, function(res){
 * 		console.log(res); // 当前请求返回值信息
 * 		});
 */
function reqGet(url, data, cb) {
    data['token'] = wx.getStorageSync('token');
    wx.request({
        url: url,
        data: data,
        method: 'get',
        header: {
            'ContentType': 'application/json'
        },
        success: function (res) {
            console.log(res);
           
            if(res.data.status == 0){
                wx.showToast({
                  title: res.data.msg,
                  icon:'none',
                  duration:1500
                })
                
            }
            if(res.data.data){
                res.data.data = formatData(res.data.data);
            }
            return typeof cb == "function" && cb(res.data)
        },
        fail: function () {
            return typeof cb == "function" && cb(false)
        }
    })
}


function reqPostToken(url, data, cb,_this) {
    data['token'] =  wx.getStorageSync('token');
    reqPost(url, data, cb,_this);
}


/**
 * url 
 *  data
 *  cb : 返回回调函数
 *  例 reqPost(url, data, function(res){
 * 		console.log(res); // 当前请求返回值信息
 * 		});
 */
function reqPost(url, data, cb,_this) {
    
    wx.request({
        url: url,
        data: data,
        method: 'post',
        header: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        success: function (res) {
            if(res.data.status == 0){
                wx.showToast({
                  title: res.data.msg,
                  icon:'none',
                  duration:1500
                })
            }else if(res.data.status == -1){
                // console.log('token过期了呢');
                // _login.loginx(_this,function(r){
                //     reqPost(url, data, cb,_this);
                // });
                wx.setStorageSync('token', "");
                console.log(res);
                _login.loginx(function(r){
                    console.log(r);
                });
            }


            if(res.data.data){
                res.data.data = formatData(res.data.data);
            }
            console.log(res.data);
            return typeof cb == "function" && cb(res.data)
        },
        fail: function () {
            return typeof cb == "function" && cb(false)
        }
    })
}
//添加finally：因为还有一个参数里面还有一个complete方法。
Promise.prototype.finally = function (callback) {
    let P = this.constructor;
    return this.then(
        value => P.resolve(callback()).then(() => value),
        reason => P.resolve(callback()).then(() => {
            throw reason
        })
    );
};

//封装异步api
const wxPromisify = fn => {
    return function (obj = {}) {
        return new Promise((resolve, reject) => {
            obj.success = function (res) {
                resolve(res)
            }

            obj.fail = function (res) {
                reject(res)
            }

            fn(obj)
        })
    }
}

const getLocationPromisified = wxPromisify(wx.getLocation); //获取经纬度
const showModalPromisified = wxPromisify(wx.showModal); //弹窗
const getSystemInfo = wxPromisify(wx.getSystemInfo); //获取系统信息
const getUserInfo = wxPromisify(wx.getUserInfo); //获取用户信息
const downloadFile = wxPromisify(wx.downloadFile); //预下载文件
// const request = wxPromisify(wx.request);//ajax请求

// 封装post请求
const post = (url, data) => {
    var promise = new Promise((resolve, reject) => {
        //网络请求
        wx.request({
            url: rootDocment + url,
            data: data,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) { //服务器返回数据
                if (res.statusCode == 200) {
                    resolve(res);
                } else { //返回错误提示信息
                    reject(res.data);
                }
            },
            error: function (e) {
                reject('网络出错');
            }
        })
    });
    return promise;
}

// 封装get请求
const get = (url, data) => {
    var promise = new Promise((resolve, reject) => {
        //网络请求
        wx.request({
            url: rootDocment + url,
            data: data,
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token')
            },
            success: function (res) { //服务器返回数据
                if (res.statusCode == 200) {
                    resolve(res);
                } else { //返回错误提示信息
                    reject(res.data);
                }
            },
            error: function (e) {
                reject('网络出错');
            }
        })
    });
    return promise;
}

/**
 * 格式化null值 都转成 空字符串
 */
function formatData(data){
    if(data){
        for (var key in data) {
            if(!data){
                data[key] = "";
            }
        }
    }
    return data;
}

/**
 * 两个map合并
 * @param {*} map1 
 * @param {*} map2 
 */
function concatMap(map1, map2) {
    for (var key in map2) {
        map1[key] = map2[key];
    }
    return map1;
}

//合并暴漏方法返回
module.exports = concatMap(exports, {
    reqPost: reqPost,
    reqGet: reqGet,
    post: reqPostToken,
    get: reqGet,
    postn:reqPost,
    path: rootDocment,
    getLocationPromisified,
    showModalPromisified,
    getSystemInfo,
    getUserInfo,
    downloadFile,
    a: "b",
    u:u,
})

